import { GET_LIST_DATA,RESET_PAGE } from '../actions/actionTypes'

const initState = {
    page: 0,
    pages: 1, // 总页数
    isend: false, // 是否加载完毕,
    list: [] // 最后得到的数据
}

const getContentList = (state, action) => {
    let page = action.obj.isToFirst ? 0 : state.page; // 当前加载的页码
    let _pages = action.obj.pages;
    let _dataList = [];
    let _isend = false;
    if(page === 0) {
        _dataList = action.obj.data.poilist
    } else {
        let beforeList = state.list;
        _dataList = action.obj.data.poilist.concat(beforeList)
    }
    let _page = ++page; // 下一次加载的页码

    if(_page >= _pages) {
        _isend = true;
    }
    return {...state,  list: _dataList, page: _page , pages: _pages, isend: _isend}
}

const resetpage = (state, action) => {
    return {...state,  page: action.obj.page}
}

const getContentListReducer = (state = initState, action) => {
    switch (action.type) {
        case GET_LIST_DATA:
            return getContentList(state, action);
        case RESET_PAGE:
            return resetpage(state, action);
        default:
            return state;
    }
}

export default  getContentListReducer;
